// pages/myCoin/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allMoney:0,
    tip:'全部提现',
    opacity:0.5,
    inputText:'',
    allCoin:0,
    isTXSuccess:false,
    currentCoin:0,
    isConfigQuestion:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isConfigQuestion: app.isConfigQuestion 
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    this.getMyUserInfo(result=>{
      // that.setData({
      //   currentCoin: result.data.currentCoin,
      //   allMoney: result.data.currentCoin/100,
      //   allCoin:result.data.currentCoin/100,
      //   inputText: result.data.currentCoin / 100,
      //   opacity:1
      // })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    this.getMyUserInfo(result => {
      wx.stopPullDownRefresh()
      // that.setData({
      //   currentCoin: result.data.currentCoin,
      //   allCoin: result.data.currentCoin/100,
      //   allMoney: result.data.currentCoin / 100,
      //   inputText: result.data.currentCoin / 100,
      //   opacity: 1
      // })
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },


  /**
   * 输入金额改变
   */ 
  inputChange(e) {
    console.log(e.detail.value)

    var all = this.data.allMoney
    if (Number(e.detail.value)) {
      let inputMoney = Number(e.detail.value)
      this.data.inputText = Number(e.detail.value)
      if (inputMoney * 100 - parseInt(inputMoney * 100) > 0) {
        this.setData({
          allCoin: parseInt(inputMoney * 100) / 100
        })
      } else {
        if (inputMoney > all) {
          this.setData({
            tip: '输入金额错误',
            opacity: 0.5
          })
        } else {
          this.setData({
            tip: '全部提现',
            opacity: 1
          })
        }
      }

    } else {
      this.setData({
        opacity: 0.5,
        tip: '输入金额错误',
      })

    }
  },

  /**
   * 点击咨询
   */
  clickYZXaction(){
    wx.navigateTo({
      url: '/pages/consultationHome/index',
    })
  },
  /**
   * 立即提现
   */
  handleSubmit(){
    var that = this
    if(this.data.opacity==0.5){
      return
    }
    if (Number(this.data.inputText)<1){
      wx.showModal({
        title: '',
        content: '金额大于1元才能提现哦',
        confirmText: '知道啦',
        showCancel: false
      })
      return
    }
    wx.showLoading({
      title: '请稍等...',
    })
    this.tx(result=>{
      wx.hideLoading()
      that.setData({
        isTXSuccess: true
      })
    })
  },


  /**
   * 全部提现
   */
  clickAllTX(){
    if (this.data.tip == '全部提现'){
      this.setData({
        allCoin: this.data.currentCoin/100
      })
    }
  },

  /**
   *  获取个人信息
  */
  getMyUserInfo(callBack){
    var that = this
    app.post(app.Apis.GET_USER_INFO,{
      userId: app.userId,
      storeId: app.mainStoreId
    },result=>{
      console.log(result)
      callBack(result)
      that.setData({
        currentCoin: result.data.currentCoin,
        allMoney: result.data.currentCoin / 100,
        allCoin: result.data.currentCoin / 100,
        inputText: result.data.currentCoin / 100,
        opacity: 1
      })
    })
  },


  /**
   * 提现接口
   */
  tx(){
    var that = this
    console.log(parseInt(that.data.inputText * 100))
    // return
    app.post(app.Apis.PUT_TX, {
      userId: app.userId,
      storeId: app.mainStoreId,
      coin: parseInt(that.data.inputText * 100)
    }, result => {
      callBack(result)
      if (result.seccess){
        that.this.getMyUserInfo(result => {

        })
      }
    })
  },


  /**
   * 查看健康币详细
   */
  lookDetailAction(){
    wx.navigateTo({
      url: '/pages/healthcoinDetail/index?id=' + false,
    })
  },
  /**
   * 回到首页
   */
  clickToHomePage(){
    wx.navigateBack({
      delta: 10
    })
  }


})